package com.mangatta.ec2.activity;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.mangatta.cloud.Metric;
import com.mangatta.cloud.Statistic;
import com.mangatta.cloud.alarm.AlarmConfiguration;
import com.mangatta.cloud.dao.AlarmConfigDao;
import com.mangatta.ec2.adaptor.AlarmConfigurationAdaptor;
import com.mangatta.ec2.adaptor.AlarmConfigurationView;
import com.mangatta.ec2.util.Util;

import java.util.ArrayList;
import java.util.List;

public class AlarmConfigurationListActivity extends ListActivity {
    private final static String TAG = AlarmConfigurationListActivity.class.getSimpleName();
    private AlarmConfigDao db;
    private EditText input;
    private AlertDialog dialog;
    private int selectedPoistion;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        db = new AlarmConfigDao(this);
        input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        setContentView(R.layout.alarm_configuration_list);
        final ListActivity l = this;
        dialog = new AlertDialog.Builder(this)
                .setTitle("Enter threshold value")
                .setMessage("Enter the threshold value")
                .setView(input)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String inputText = input.getText().toString();
                        Double threshold = Util.isEmpty(inputText) ? null : Double.valueOf(inputText);
                        ((AlarmConfiguration) l.getListView().getItemAtPosition(selectedPoistion)).setThreshold(threshold);
                        AlarmConfigurationAdaptor adapter = (AlarmConfigurationAdaptor) getListAdapter();
                        adapter.notifyDataSetChanged();
                    }
                }).create();
    }


    private List<AlarmConfiguration> createTestAlarmConfigurations() {
        String instanceId = getInstanceIdFromIntent();
        List<AlarmConfiguration> alarmConfigurations = getAlarmConfigList(instanceId);

        Log.d(TAG, "Size in db");

        return alarmConfigurations;
    }

    private String getInstanceIdFromIntent() {
        Bundle extras = this.getIntent().getExtras();
        return extras.getString(Util.INSTANCE_ID);
    }

    private List<AlarmConfiguration> getAlarmConfigList(String instanceId) {
        List<AlarmConfiguration> dbList = db.getAlarmConfigsForInstance(instanceId);
        if (!dbList.isEmpty()) {
            return dbList;
        }
        return createDefaultAlarms(instanceId);
    }

    private List<AlarmConfiguration> createDefaultAlarms(String instanceId) {
        List<AlarmConfiguration> configList = new ArrayList<AlarmConfiguration>();
        for (Metric metric : Metric.values()) {
            AlarmConfiguration alarmConfig = new AlarmConfiguration(instanceId, metric, Statistic.getDefault(), null);
            configList.add(alarmConfig);
        }
        return configList;
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        setListAdapter(new AlarmConfigurationAdaptor(this, createTestAlarmConfigurations()));
    }


    public void saveAlarmConfig(View button) {
        ListView listView = getListView();
        int count = listView.getCount();
        List<AlarmConfiguration> alarmConfigs = new ArrayList<AlarmConfiguration>();
        while (--count >= 0) {
            AlarmConfigurationView rowView = (AlarmConfigurationView) listView.getChildAt(count).getTag();
            Double threshold = Util.isEmpty(rowView.threshold.getText().toString()) ? null : Double.valueOf(rowView.threshold.getText().toString());
            AlarmConfiguration alarmConfig = new AlarmConfiguration(getInstanceIdFromIntent(),
                    Metric.fromName(rowView.alarmName.getText().toString()),
                    Statistic.getDefault(), threshold);
            alarmConfigs.add(alarmConfig);
        }
        //save the bean values in db.
        db.saveAlarmConfig(alarmConfigs);

        finish();
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Log.d(TAG, "on click list");
        TextView thresholdview = (TextView) v.findViewById(R.id.alarm_threshold);
        input.setText(thresholdview.getText());
        selectedPoistion = position;
        dialog.show();

    }

}
